﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RSA加密与解密
{
    class Program
    {
        static void Main(string[] args)
        {
            //RSATool myRSA = new RSATool();
            //Dictionary<string, string> dictK = new Dictionary<string, string>();
            //dictK = myRSA.GetKey();
            ////Console.WriteLine("公钥字符串：{0}", dictK["PublicKey"]);
            //Debug.WriteLine("公钥字符串：{0}", dictK["PublicKey"]);
            ////Console.WriteLine("私钥字符串：{0}", dictK["PrivateKey"]);
            //Debug.WriteLine("私钥字符串：{0}", dictK["PrivateKey"]);
            //string strText = "123456";
            //Console.WriteLine("要加密的字符串是：{0}", strText);
            //string str1 = myRSA.Encrypt("123456", dictK["PublicKey"]);
            //Console.WriteLine("加密后的字符串：{0}", str1);
            //string str2 = myRSA.Decrypt(str1, dictK["PrivateKey"]);
            //Console.WriteLine("解密后的字符串：{0}", str2);

            //string priKeyPem = @"-----BEGIN RSA PRIVATE KEY-----
            //    MIICWwIBAAKBgQCf1a4LQyipBqeUCZ9kKsfasQzkEFCBmGsM21Sakb5BO0sY07GD
            //    cproJHF2xNQrV0cM7+liE3pBUFsarui2WaHZhAibpLbl9z4FSfoN5hSg6sEgbB17
            //    SvKe3ZN/75GoEsQiQtYW4gUJgzrBovVZ+TeTnN+NHHBqUqBKhNIgPFVapQIDAQAB
            //    AoGAG0OMs5kaF3LuJN9bU+/ENXab908dHG4OXJwRG2ie5muhzLNXhU+IQu7sd9Dt
            //    TBNQKFHIIpWl9fwp/iw1v90cMUQGj0zhSXHAz7Vak/ryQLTyeIIciL8MQWvnbAaN
            //    lIoFq2wBl7SYs3n71B4MlvvTysaG0krsjiPh5LVgnBvzjGECQQDcAwe4XnF7SHWO
            //    nfljrG29soKNiUhYKtDGcV9fvam9u50Ek882wvFmsJP+tk+1CXjMRSNlOi40bxKC
            //    uaBa1JOtAkEAufq9FmZHfBFf3e6n57wLiAj5C1MeyHAtt6qdAF49OZJBGZh1pePn
            //    jDGNezFvy7U5bMp7/updisLCFueS5eKB2QJAF84QIMe/OZqedZ7sI/e9LABLlerb
            //    tAZ17nLH4gEQg6HwHFWt3vv6yKSkbrPlLe5nbpqweLxx0WSPOSvCiPFlRQJAPAfF
            //    NQ+6jz+EdDxukgxOpJBQ4ujnjMc42ooFt3KzzHt66+ocP3m66bOs+VDRxy0t5gHN
            //    2FCJ9Ro8T+xbrDxasQJAARHpcG6tE0F+lmUthtep1U8OrF+AQvqDhBq8MYK+/pF/
            //    LRZkFHkqTsj89OyWDlSH3LeYkOWsr9mAFxsvHZ9BSA==
            //    -----END RSA PRIVATE KEY-----";

            //1、先输入""
            //2、把Pem文件里的内容复制过来
            //3、在前面加上@符号
            //string priKeyPem = @"-----BEGIN RSA PRIVATE KEY-----
            //                    MIICXAIBAAKBgQD6PjEDGPwHT5hpG2hoAi1AI94RsR566wNT88tJSpVXOWh4/YZP
            //                    Rn/5M5TojSD6wH5IwvS4CQ27QGkeK53BJkR4xMKRQ+btkslPCl3EXZiajzhynumX
            //                    vPM+MMXg6Z/yADNz5nxMIs66k+1HyS8qPjId1d8PMQtKvhLs6svYeQf+PwIDAQAB
            //                    AoGAaAT2Bp2aFcfmSLqV2bYd7S17n8sKKnoKNhl/BQY1sb4xSSnJbKaXh5UDbu9L
            //                    KDBfzgsMAnvw80dA6sOinak0iFSFOeqYkmO3DDVW+7ulCfMaxHrZe8COHc7AOP/N
            //                    UUMW5r2pkep+LKhJsbfVDQ6bBCQM3/RySFUfBX4pUzbrFVECQQD+S98jTo/mK9/8
            //                    kK9udmoQvEeOORKSAgrbU3NA314vNrAl6Cza5UZPxXglreYG8DbEuYMj+7kxS2EX
            //                    vRVGyvblAkEA++teNiXcSkmSzCQD3OB4Bk5jgjo9nd8xJT0hJYHZolZ9wZcwjW6V
            //                    ale33jsF5YApa50nwEhzxCd3tAUu4vEKUwJANPb0iNQKILSuuFoiT+2V9dphRPwa
            //                    JBMM09WzwaVEZq0LTznmbcknNeI5Pg/UQ0mw/pRs/gel8OB0Xo9haRdJCQJAL1l3
            //                    5bmMBDJ78v658yQFqznEnk+QEUJIHHW26zPS0h5rmJ1wNMUaNFi0aIKzgwVpw55Q
            //                    QGXdzHqWlzF6NJGVnQJBAM6vgdM2Q9OwVhXvf0g0dGukD/DGqqF8e1pZGTosBY5z
            //                    4dRQamzJVel8L3KYaZwAPLNP6Y4jS1NQdVdtov616qw=
            //                    -----END RSA PRIVATE KEY-----";
            

            RSATool rsa = new RSATool();
            Dictionary<string, string> dictK = new Dictionary<string, string>();
            Console.WriteLine("PubKey:" + rsa.GetPublicKey(rsa.PriKey));
            Console.WriteLine("PriKey:" + rsa.GetPriKey(rsa.PriKey));
            dictK.Add("PublicKey", rsa.GetPublicKey(rsa.PriKey));
            dictK.Add("PrivateKey", rsa.GetPriKey(rsa.PriKey));
            string strText = "abcdefg8754612dc";
            Console.WriteLine("要加密的字符串是：{0}", strText);
            string str1 = rsa.Encrypt(strText, dictK["PublicKey"]);
            Console.WriteLine("加密后的字符串：{0}", str1);
            Console.WriteLine("加密后的字符串长度：{0}", str1.Length.ToString());
            char[] chars = str1.ToCharArray();
            for (int index = 0; index < str1.Length; index++) {
                Console.WriteLine("第" + (index+1).ToString() + "个字符：" + chars[index]);
            }
            string str2 = rsa.Decrypt(str1, dictK["PrivateKey"]);
            Console.WriteLine("解密后的字符串：{0}", str2);

            Console.ReadKey();
        }
    }
}
